package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Counselor;
import org.example.studentsystem_back.domain.Leaders;
import org.example.studentsystem_back.domain.Users;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.mapper.UsersMapper;
import org.example.studentsystem_back.service.CounselorService;
import org.example.studentsystem_back.mapper.CounselorMapper;
import org.example.studentsystem_back.util.JwtUtil;
import org.example.studentsystem_back.vo.counselorMangeVo.CounselorVo;
import org.example.studentsystem_back.vo.userManageVo.PageCounselorVo;
import org.example.studentsystem_back.vo.userManageVo.smallVo.PageCounselorItem;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【counselors】的数据库操作Service实现
* @createDate 2024-07-10 08:33:08
*/
@Service
public class CounselorServiceImpl extends ServiceImpl<CounselorMapper, Counselor>
    implements CounselorService{
    @Resource
    CounselorMapper counselorMapper;
    @Resource
    UsersMapper usersMapper;

    @Override
    public CommonResponse<List<Counselor>> getAllCounselor() {
        List<Counselor> counselorList = counselorMapper.selectList(null);
        if(counselorList.isEmpty())
        {
            return CommonResponse.createForSuccess("没有辅导员信息",null);
        }
        return CommonResponse.createForSuccess("查询成功",counselorList);
    }
    @Override
    public CommonResponse<CounselorVo> getCounselorByUserId(String userId) {
        QueryWrapper<Counselor> counselorQueryWrapper=new QueryWrapper<>();
        counselorQueryWrapper.eq("userId",userId);
        Counselor counselor = counselorMapper.selectOne(counselorQueryWrapper);
        if(counselor == null)
        {
            return CommonResponse.createForSuccess("没有该userId的辅导员信息",null);
        }
        Users user = usersMapper.selectById(userId);
        CounselorVo counselorVo = counselorToCounselorVo(counselor,user);
        return CommonResponse.createForSuccess("查询成功",counselorVo);
    }
    @Override
    public CommonResponse<CounselorVo> updateCounselor(String userId, Counselor counselor) {
        return null;
    }

    //分页查询
    @Override
    public CommonResponse<PageCounselorVo> getAllCounselorByPage(PageDTO pageDto) {
        IPage<Counselor> page = new Page<>(pageDto.getPage(),pageDto.getLimit());
        List<Counselor> counselorList =counselorMapper.selectList(page,null);
        Long total = counselorMapper.selectCount(null);
        List<PageCounselorItem> itemList = new ArrayList<>();
        counselorList.forEach(counselor -> {
            PageCounselorItem item = new PageCounselorItem();
            item.setDataByCounselor(counselor);
            item.setName(usersMapper.selectById(counselor.getUserid()).getName());
            itemList.add(item);
        });
        PageCounselorVo pageCounselorVo = new PageCounselorVo();
        pageCounselorVo.setPageList(itemList);
        pageCounselorVo.setTotal(total);
        return CommonResponse.createForSuccess("查询成功", pageCounselorVo);
    }

    @Override
    public CommonResponse<CounselorVo> getCurrentCounselor(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        JwtUtil.verifyToken(token);
        Users user = JwtUtil.getCurrentUser(token);
        QueryWrapper<Counselor> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            queryWrapper.eq("userid",user.getUserid());
        }
        Counselor counselor = counselorMapper.selectOne(queryWrapper);
        if(counselor==null)
        {
            return CommonResponse.createForSuccess("没有该辅导员信息",null);
        }
        CounselorVo counselorVo = counselorToCounselorVo(counselor,user);

        return CommonResponse.createForSuccess("查询成功",counselorVo);
    }

    private CounselorVo counselorToCounselorVo(Counselor counselor, Users user) {
        CounselorVo counselorVo = new CounselorVo();
        counselorVo.setEmployeeId(counselor.getEmployeeId());
        counselorVo.setUserid(counselor.getUserid());
        counselorVo.setClassName(counselor.getClassName());
        counselorVo.setDepartment(counselor.getDepartment());
        counselorVo.setPhone(counselor.getPhone());
        counselorVo.setOfficeAddress(counselor.getOfficeAddress());
        counselorVo.setWorkExperience(counselor.getWorkExperience());
        counselorVo.setTitle(counselor.getTitle());
        counselorVo.setHireDate(counselor.getHireDate());

        counselorVo.setPassword(user.getPassword());
        counselorVo.setName(user.getName());
        counselorVo.setGender(user.getGender());
        counselorVo.setRole(user.getRole());

        return counselorVo;
    }
}




